package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlPost entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlPost
 * @author MyEclipse Persistence Tools
 */

public class MdlPostDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(MdlPostDAO.class);
	// property constants
	public static final String MODULE = "module";
	public static final String USERID = "userid";
	public static final String COURSEID = "courseid";
	public static final String GROUPID = "groupid";
	public static final String MODULEID = "moduleid";
	public static final String COURSEMODULEID = "coursemoduleid";
	public static final String SUBJECT = "subject";
	public static final String SUMMARY = "summary";
	public static final String CONTENT = "content";
	public static final String UNIQUEHASH = "uniquehash";
	public static final String RATING = "rating";
	public static final String FORMAT = "format";
	public static final String SUMMARYFORMAT = "summaryformat";
	public static final String ATTACHMENT = "attachment";
	public static final String PUBLISHSTATE = "publishstate";
	public static final String LASTMODIFIED = "lastmodified";
	public static final String CREATED = "created";
	public static final String USERMODIFIED = "usermodified";

	public void save(MdlPost transientInstance) {
		log.debug("saving MdlPost instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MdlPost persistentInstance) {
		log.debug("deleting MdlPost instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlPost findById(java.lang.Long id) {
		log.debug("getting MdlPost instance with id: " + id);
		try {
			MdlPost instance = (MdlPost) getSession().get(
					"idv.loveloser.dao.MdlPost", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlPost instance) {
		log.debug("finding MdlPost instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlPost")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlPost instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MdlPost as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByModule(Object module) {
		return findByProperty(MODULE, module);
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByCourseid(Object courseid) {
		return findByProperty(COURSEID, courseid);
	}

	public List findByGroupid(Object groupid) {
		return findByProperty(GROUPID, groupid);
	}

	public List findByModuleid(Object moduleid) {
		return findByProperty(MODULEID, moduleid);
	}

	public List findByCoursemoduleid(Object coursemoduleid) {
		return findByProperty(COURSEMODULEID, coursemoduleid);
	}

	public List findBySubject(Object subject) {
		return findByProperty(SUBJECT, subject);
	}

	public List findBySummary(Object summary) {
		return findByProperty(SUMMARY, summary);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByUniquehash(Object uniquehash) {
		return findByProperty(UNIQUEHASH, uniquehash);
	}

	public List findByRating(Object rating) {
		return findByProperty(RATING, rating);
	}

	public List findByFormat(Object format) {
		return findByProperty(FORMAT, format);
	}

	public List findBySummaryformat(Object summaryformat) {
		return findByProperty(SUMMARYFORMAT, summaryformat);
	}

	public List findByAttachment(Object attachment) {
		return findByProperty(ATTACHMENT, attachment);
	}

	public List findByPublishstate(Object publishstate) {
		return findByProperty(PUBLISHSTATE, publishstate);
	}

	public List findByLastmodified(Object lastmodified) {
		return findByProperty(LASTMODIFIED, lastmodified);
	}

	public List findByCreated(Object created) {
		return findByProperty(CREATED, created);
	}

	public List findByUsermodified(Object usermodified) {
		return findByProperty(USERMODIFIED, usermodified);
	}

	public List findAll() {
		log.debug("finding all MdlPost instances");
		try {
			String queryString = "from MdlPost";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlPost merge(MdlPost detachedInstance) {
		log.debug("merging MdlPost instance");
		try {
			MdlPost result = (MdlPost) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MdlPost instance) {
		log.debug("attaching dirty MdlPost instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MdlPost instance) {
		log.debug("attaching clean MdlPost instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}